package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.web.pojo.InverseKeywordInfo;
import com.ys.web.pojo.MainBussinessInfo;
import com.ys.web.service.MainBussinessService;

@Service
@Transactional
public class MainBussinessServiceImpl implements MainBussinessService {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Override
	public String addMainBussiness(String industryId, String industryName,
			String mainBussiness) {
		String sql = "select * from mainBussiness where industryId='"
				+ industryId + "' and mainBussiness='" + mainBussiness + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if (list.isEmpty()) {
			sql = "insert into mainBussiness(industryId, industryName, mainBussiness) values('"
					+ industryId
					+ "','"
					+ industryName
					+ "','"
					+ mainBussiness
					+ "')";
			int result = jdbcTemplate.update(sql);
			if (result > 0) {
				return "添加成功";
			} else {
				return "添加失败";
			}
		}
		return industryName + "行业已有此主营业务";

	}

	@Override
	public List<MainBussinessInfo> getMainBussinessByIndustryId(
			String industryId) {
		List<MainBussinessInfo> list = new ArrayList<MainBussinessInfo>();
		String sql = "select * from mainBussiness where industryId='"
				+ industryId + "'";
		List<Map<String, Object>> listMap = jdbcTemplate.queryForList(sql);
		if (!listMap.isEmpty()) {
			for (int i = 0; i < listMap.size(); i++) {
				MainBussinessInfo mainBussinessInfo = new MainBussinessInfo();
				mainBussinessInfo.setId((int) listMap.get(i).get("id"));
				mainBussinessInfo.setIndustryId((String) listMap.get(i).get(
						"industryId"));
				mainBussinessInfo.setIndustryName((String) listMap.get(i).get(
						"industryName"));
				mainBussinessInfo.setMainBussiness((String) listMap.get(i).get(
						"mainBussiness"));
				list.add(mainBussinessInfo);
			}
			return list;
		}

		return null;
	}

	@Override
	public String addInverse(String industryId, String industryName, int mid,
			String mainBussiness, String inverseKeyword, String matchedPattern) {
		String sqlindex = "select inverseKeyword,matchedPattern from inverseKeyword where industryId='"
				+ industryId + "' and mainBussiness='" + mainBussiness + "'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlindex);
		String r1 = "";
		String r2 = "";
		if (!list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				r1 = (String) list.get(i).get("inverseKeyword");
				r2 = (String) list.get(i).get("matchedPattern");
				if (r1.equals(inverseKeyword)
						&& r2.equals(matchedPattern)) {
					return "已存在此否词与匹配模式";
				}
			}
		} else {
			StringBuffer sql = new StringBuffer();
			sql.append(
					"insert into inverseKeyword(industryId,industryName,mid,mainBussiness,inverseKeyword,matchedPattern) values('")
					.append(industryId).append("','").append(industryName)
					.append("','").append(mid).append("','")
					.append(mainBussiness).append("','").append(inverseKeyword)
					.append("','").append(matchedPattern).append("')");
			int result = jdbcTemplate.update(sql.toString());
			if (result > 0) {
				return "添加成功";
			}

		}

		return "添加失败";

	}

	@Override
	public List<InverseKeywordInfo> getInverse(String industryId,
			String mainBussiness) {
		String sql = "select * from inverseKeyword where industryId='"
				+ industryId + "' and mainBussiness='" + mainBussiness + "'";
		List<Map<String, Object>> listMap = jdbcTemplate.queryForList(sql);
		List<InverseKeywordInfo> list = new ArrayList<InverseKeywordInfo>();
		if (!listMap.isEmpty()) {
			for (int i = 0; i < listMap.size(); i++) {
				InverseKeywordInfo inverseKeywordInfo = new InverseKeywordInfo();
				inverseKeywordInfo.setId((int) listMap.get(i).get("id"));
				inverseKeywordInfo.setIndustryId((String) listMap.get(i).get(
						"industryId"));
				inverseKeywordInfo.setIndustryName((String) listMap.get(i).get(
						"industryName"));
				inverseKeywordInfo.setMainBussiness((String) listMap.get(i)
						.get("mainBussiness"));
				inverseKeywordInfo.setInverseKeyword((String) listMap.get(i)
						.get("inverseKeyword"));
				inverseKeywordInfo.setMatchedPattern((String) listMap.get(i)
						.get("matchedPattern"));
				list.add(inverseKeywordInfo);
				return list;
			}
		}
		return null;
	}

	@Override
	public MainBussinessInfo getMainBussinessById(Integer id) {
		String sql = "select mainBussiness from mainBussiness where id = "+id;
		List<Map<String, Object>> listMap = jdbcTemplate.queryForList(sql);
		
		if(listMap.isEmpty()){
			return null;
		}
			MainBussinessInfo m = new MainBussinessInfo();
			m.setId(id);
			m.setIndustryId((String) listMap.get(0).get("industryId"));
			m.setIndustryName((String) listMap.get(0).get("industryName"));
			m.setMainBussiness((String) listMap.get(0).get("mainBussiness"));
			
		
		return m ;
	}

}
